<template>
    <div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="name">
                <el-input type="text" v-model.number="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="手机号码" prop="tel">
                <el-input type="text" v-model="ruleForm.tel" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- <v-loader :source="msg" :urls="['/']">
            <div @click="getRoot">{{ msg }}</div>
        </v-loader> -->
    </div>
</template>
<script>
    import axios from "axios";
    export default {
        //name: "test",
        data() {
            var checkName = (rule, value, callback) => {
                var regName = /^[a-z_]{1}[a-zA-Z0-9_]{5,11}$/i;
                if (!regName.test(value)) {
                    callback(new Error('用户名格式错误'));
                } else {
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var validateTel = (rule, value, callback) => {
                let regTel = /^[1]{1}[34578]{1}[0-9]{9}$/;
                if (value === '') {
                    callback(new Error('请输入手机号码'));
                } else {
                    if (!regTel.test(value)) {
                        callback(new Error('手机号码格式错误'));
                    } else {
                        callback();
                    }
                }
            };
            return {
                msg: "",
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    tel: '',
                    name: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    name: [
                        { validator: checkName, trigger: 'change' }
                    ],
                    tel: [
                        { validator: validateTel, trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },

            getRoot() {
                axios.get("/").then(() => {
                    this.msg = "click to test" + Math.floor(Math.random() * 100);
                });
            }
        },
        mounted(){
            this.getRoot();
        }
    }
</script>
<style>
    .el-dropdown-link {
        cursor: pointer;
        color: #409EFF;
    }

    .el-icon-arrow-down {
        font-size: 12px;
    }
</style>